package com.chushouya.manager.dao.repository;

import com.general.framework.core.lang.Integers;
import com.general.framework.core.lang.Longs;
import com.general.framework.core.lang.Numbers;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.manager.dao.entity.BannerEntity;
import com.chushouya.manager.dao.mapper.BannerEntityMapper;
import com.chushouya.manager.dto.admin.banner.BannerQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Article repository for database operations
 */
@Repository
public class BannerRepository extends BaseRepository<BannerEntityMapper, BannerEntity,Long> {

    /**
     * 查询文章分页列表
     *
     * @param query 查询条件
     * @return 分页列表
     */
    public PageResult<BannerEntity> selectPage(BannerQuery query) {
        return selectPage(query, buildExample(query));
    }

    public List<BannerEntity> selectList(BannerQuery query) {
        return selectByExample(buildExample(query));
    }



    /**
     * 查询文章分页列表
     *
     * @param query 查询条件
     * @return 分页列表
     */
    public BannerEntity selectOne(BannerQuery query) {
        return selectFirstOneByExample(buildExample(query));
    }

    private Example buildExample(BannerQuery query) {
        Example example = buildExample();
        Example.Criteria c = example.and();
        c.andEqualTo("delFlag", Numbers.INTEGER_ZERO);
        if (Longs.notNullAndZero(query.getPositionId())) {
            c.andEqualTo("positionId", query.getPositionId());
        }
        if (Integers.notNullAndZero(query.getStatus())) {
            c.andEqualTo("status", query.getStatus());
        }

        if(Strings.isNotEmpty(query.getPlatform())){
            c.andEqualTo("platform",query.getPlatform());
        }
        
        // 添加时间范围条件，确保只查询有效期内的Banner
        if (query.getStartTime() != null) {
            c.andLessThanOrEqualTo("startTime", query.getStartTime());
        }
        if (query.getEndTime() != null) {
            c.andGreaterThanOrEqualTo("endTime", query.getEndTime());
        }
        example.and().andEqualTo("delFlag", Numbers.INTEGER_ZERO);
        example.orderBy("sort").orderBy("bannerId").desc();
        return example;
    }
}
